/*
 * @(#)XSLT.java	1.0 Apr 26, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.xml;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.XMLFilter;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import java.util.*;
import java.io.*;

public class XSLT
{
	protected InputSource inXml;
	protected SAXParserFactory spf;
	protected SAXParser parser;
	protected XMLReader reader;
	protected SAXTransformerFactory stf;
	protected StreamResult outXml;
	protected ArrayList<XMLFilter> filters;

	public static void main(String[] args)
	{
		if(args.length < 3)
		{
			System.out.println("USAGE: java org.eoti.xml.XSLT inXml [[inXsl]] outXml");
			System.out.println("Where inXsl can be any number of stylesheets.");
			System.exit(0);
		}

		try{
	    	XSLT xslt = new XSLT();
	    	int inIndex = 0;
	    	int outIndex = args.length - 1;
	    	xslt.setXmlIn(args[inIndex]);
	    	System.out.println("XML In: " + args[inIndex]);
	    	System.out.println("Stylesheets: ");
			for(int i=inIndex+1; i<outIndex; i++)
			{
				xslt.addXsl(args[i]);
				System.out.println("\t" + args[i]);
			}

			xslt.setXmlOut(args[outIndex]);
			System.out.println("XML Out: " + args[outIndex]);
			xslt.transform();
			System.out.println("Transformation complete.");
	    }catch(Exception e){
	    	System.out.format("Exception: %s", e.getMessage());
	    	e.printStackTrace();
	    }
	}

	public XSLT()
	throws XMLException
	{
		try{
			spf = SAXParserFactory.newInstance();
			spf.setNamespaceAware(true);
			parser = spf.newSAXParser();
			reader = parser.getXMLReader();
			stf = (SAXTransformerFactory) TransformerFactory.newInstance();
			filters = new ArrayList<XMLFilter>();
	    }catch(Exception e){
	    	throw new XMLException(e);
	    }
	}

	public void setXmlIn(String fileName)
	throws XMLException
	{
		try{
	    	setXmlIn(new File(fileName));
	    }catch(Exception e){
	    	throw new XMLException(e);
	    }
	}

	public void setXmlIn(File file)
	throws XMLException
	{
		try{
			setXmlIn(new FileInputStream(file));
	    }catch(Exception e){
	    	throw new XMLException(e);
	    }
	}

	public void setXmlIn(InputStream in)
	throws XMLException
	{
		try{
			setXmlIn(new InputSource(new BufferedInputStream(in)));
	    }catch(Exception e){
	    	throw new XMLException(e);
	    }
	}

	public void setXmlIn(InputSource source)
	{
		this.inXml = source;
	}

	public void addXsl(XMLFilter filter)
	{
		filters.add(filter);
	}

	public void addXsl(StreamSource source)
	throws XMLException
	{
		try{
	    	addXsl(stf.newXMLFilter(source));
	    }catch(Exception e){
	    	throw new XMLException(e);
	    }
	}

	public void addXsl(File file)
	throws XMLException
	{
		try{
	    	addXsl(new StreamSource(file));
	    }catch(Exception e){
	    	throw new XMLException(e);
	    }
	}

	public void addXsl(String fileName)
	throws XMLException
	{
		try{
	    	addXsl(new File(fileName));
	    }catch(Exception e){
	    	throw new XMLException(e);
	    }
	}

	public void setXmlOut(String fileName)
	throws XMLException
	{
		try{
	    	setXmlOut(new File(fileName));
	    }catch(Exception e){
	    	throw new XMLException(e);
	    }
	}

	public void setXmlOut(File file)
	throws XMLException
	{
		try{
	    	setXmlOut(new StreamResult(file));
	    }catch(Exception e){
	    	throw new XMLException(e);
	    }
	}

	public void setXmlOut(StreamResult result)
	{
		this.outXml = result;
	}

	public void setXmlOut(OutputStream stream)
	throws XMLException
	{
		try{
	    	setXmlOut(new StreamResult(stream));
	    }catch(Exception e){
	    	throw new XMLException(e);
	    }
	}

	public void setXmlOut(Writer writer)
	throws XMLException
	{
		try{
	    	setXmlOut(new StreamResult(writer));
	    }catch(Exception e){
	    	throw new XMLException(e);
	    }
	}

	public void transform()
	throws XMLException
	{
		try{
	    	XMLReader last = reader;
	    	for(XMLFilter filter : filters)
	    	{
	    		filter.setParent(last);
	    		last = filter;
	    	}

			Transformer transformer = stf.newTransformer();
			SAXSource source = new SAXSource(last, inXml);
			transformer.transform(source, outXml);
	    }catch(Exception e){
	    	throw new XMLException(e);
	    }
	}
}